<?php
/**
 * Created by PhpStorm.
 * User: Administrator
 * Date: 2021/7/27
 * Time: 14:19
 */

namespace app\api\controller;



use app\model\UserModel;
use think\facade\Request;
use think\facade\Validate;

class User
{
    //添加用户
    public function add()
    {
        // 1. 获取参数
        // 2. 校验参数
        // 3. 验证文本是否合规。
        // 4. 写入数据库
        // 5. 返回数据

        $params = Request::param();
        $validate = Validate::rule([
            'avatar' => 'require',
            'nickname' => 'require',
            'openid' => 'require'
        ]);
        if (!$validate->check($params)) {
            $data = [
                'status' => 10002,
                'message' => $validate->getError(),
                'data' => []
            ];
            return json($data);
        }

//        //
//        echo getcwd(); // current work directory
//        exit();
        // 验证评论内容是否合规
//        require_once '../extend/baiduai/AipContentCensor.php';
//        $client = new \AipContentCensor(24142794, "azoIXPHEO3lu4izsRldsDDG9", "L9MNwqC652eviUTPGlFNQZEq4rGSuOaE");
//        $result = $client->textCensorUserDefined($params['comment_content']. ' '. $params['email']. ' '. $params['nickname']);
//        if ($result['conclusionType'] == 2) {
//            $data = [
//                'status' => 10003,
//                'message' => '评论内容包含敏感词',
//                'data' => []
//            ];
//            return json($data);
//        }
//        if ($result['conclusionType'] == 3) { // 疑似
//            $insertData = $params;
//            $insertData['add_time'] = $insertData['update_time'] = time();
//            $insertData['status'] = 2;
//            CommentModel::create($insertData);
//            $data = [
//                'status' => 10003,
//                'message' => '评论内容包含敏感词',
//                'data' => []
//            ];
//            return json($data);
//        }
        $insertData = $params;
        $user=UserModel::where("open_id","=",$params['openid'])->find();
        if (!$user){
            $insertData['open_id']=$params['openid'];
            $insertData['add_time'] = $insertData['update_time'] = time();
            $result = UserModel::create($insertData);
            if ($result) {
                $data = [
                    'status' => 0,
                    'message' => '',
                    'data' => [
//                    'result' => true
                        'user' => $result->toArray()
                    ]
                ];
                return json($data);
            }
        }
        $data = [
            'status' => 10001,
            'message' => '系统错误，数据库处理失败',
            'data' => []
        ];
        return json($data);
    }
    public function openId(){
        $code=Request::param('code');
        $a = file_get_contents("https://api.weixin.qq.com/sns/jscode2session?appid=wx4f8be5dead389437&secret=24d480d2bf4b6c4d8f724f20cc9edeb3&js_code=".$code."&grant_type=authorization_code");
        $re = json_decode($a,true);
        if ($re) {
                $data = [
                    'status' => 0,
                    'message' => '',
                    'data' => [
                        'openid' => $re
                    ]
                ];
                return json($data);
            }
    }
}